<?php
/**
*
* @package acp
* @version $Id: acp_menu.php 0 4 jan 2009 12:01:14 Alexander de Jong $
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/**
* @ignore 
*/
//require($phpbb_root_path . 'includes/lexportal/functions_menu.' . $phpEx);
// $menu = new menu_handler();

/**
* @package acp
*/
class acp_menu
{
	var $u_action;
	var $_menu;
	
	function main($id, $mode)
	{
		global $menu, $user;
		
		$user->add_lang('acp/lexportal_menu');
		
		$menu->load_items();
		
		$action = request_var('action', 'overview');
		
		switch($mode)
		{
			case 'menu_overview':
				switch($action)
				{
					case 'overview':
						$this->_overview();
					break;
					
					case 'create':
					case 'edit':
						$this->_creation($action);
					break;
					
					case 'move_up':
					case 'move_down':
						$this->_move_item($action);
					break;
					
					case 'delete':
						$this->_delete($id, $mode);
					break;
					
					default:
						$this->_overview();
					break;
				}
			break;
		}
	}
	
	
	function _overview()
	{
		global $template, $menu, $admin_root_path, $user;
		
		add_form_key('create_new_menu');
		
		if(!sizeof($menu->menus))
		{
			$menu->load_items();
		}
		
		if(!sizeof($menu->menus))
		{
			$template->assign_vars(array(
				'NO_MENUS'			=> true,
				'NO_MENU_EXPLAIN'	=> sprintf($user->lang['NO_MENU_EXPLAIN'], $this->u_action . '&amp;action=create'),
			));
		}
		else
		{
			for($i = 0; $i < count($menu->menus); $i++)
			{
				// $names[] = $this->_status($i);
				$template->assign_block_vars('menu', array(
					'NAME'			=> $menu->menus[$i]['name'],
					'LINK'			=> $menu->menus[$i]['link'],
					'LINK_DB'		=> $menu->menus[$i]['link_db'],
					'STATUS'		=> $user->lang['STATUS_' . $this->_status($i)],
					
					'U_EDIT'		=> $this->u_action . '&amp;action=edit&amp;p=' . $i,
					'U_DELETE'		=> $this->u_action . '&amp;action=delete&amp;m=' . $menu->menus[$i]['id'],
					'U_MOVE_UP'		=> $this->u_action . '&amp;action=move_up&amp;m=' . $menu->menus[$i]['id'],
					'U_MOVE_DOWN'	=> $this->u_action . '&amp;action=move_down&amp;m=' . $menu->menus[$i]['id'],
				
					'S_LINKIMAGE'	=> $admin_root_path . 'images/icon_folder_link.gif',
				
					'S_TOP'			=> ($menu->menus[$i]['order'] == 1) ? true : false,
					'S_BOTTOM'		=> ($menu->menus[$i]['order'] == $menu->get_highest_position()) ? true : false,
				
					'NO_MENUS'		=> false,
				));
			}
		}
		
		// print "<pre>" . print_r($names, true) . "</pre>";
		// die();
		
		$template->assign_var('U_ACTION', $this->u_action);
		
		$this->tpl_name = "acp_menu_overview";
		$this->page_title = $user->lang['MENU_OVERVIEW'];
	}
	
	function _status($menuPos, $return_html_indentifier = false)
	{
		global $menu;
		if($menu->menus[$menuPos]['not_created'] == 1)
		{
			if($return_html_indentifier)
			{
				return 2;
			}
			return "NOT_CREATED";
		}
		elseif($menu->menus[$menuPos]['disabled'] == 1)
		{
			if($return_html_indentifier)
			{
				return 3;
			}
			return "DISABLED";
		}
		
		if($return_html_indentifier)
		{
			return 1;
		}
		
		return "ACTIVE";
	}
	
	function _creation($action)
	{
		global $template, $menu, $user;
		
		$submit = (isset($_POST['submit']) ? true : false);
		
		switch($action)
		{
			case 'edit':
				$id = request_var('p', 0);
				$this->page_title = $user->lang['MENU_EDIT'];
				
				if($submit)
				{
					if(!check_form_key('form_menu'))
					{
						trigger_error('INVALID_FORM');
					}
					
					$menu_name 			= request_var('menu_name', "");
					$menu_link 			= request_var('menu_link', "");
					$menu_active_check	= request_var('menu_active_check', "");
					$status				= request_var('menu_status', 'active');
					
					$errors = false;
					
					if(!$menu_name)
					{
						$errors = true;
					}
					
					if(!$menu_link)
					{
						$errors = true;
					}
					
					if(!$menu_active_check)
					{
						$errors = true;
					}
					
					if(!$errors)
					{
						// $place = $this->get_highest_position()+1;

						$sql_array = array(
							'menu_name'			=> $menu_name,
							'menu_link'			=> $menu_link,
							'menu_active_check'	=> $this->_active_check_encode($menu_active_check),
							'menu_disabled'		=> ($status == 'disabled') ? true : false,
							'menu_not_created'	=> ($status == 'not_created') ? true : false,
						);
						
						$edit_menu = $menu->update_item($menu->menus[$id]['id'], $sql_array);
						
						if(!$edit_menu)
						{
							trigger_error(sprintf($user->lang['MENU_ERROR_EDIT'], adm_back_link($this->u_action . '&amp;action=edit&amp;p=' . $id)), E_USER_WARNING);
						}
						
						trigger_error(sprintf($user->lang['MENU_SUCCESS_EDIT'], adm_back_link($this->u_action)));
					}
				}
				
				$template->assign_vars(array(
					'MENU_NAME'					=> $menu->menus[$id]['name_var'],
					'MENU_LINK'					=> $menu->menus[$id]['link_db'],
					'MENU_ACTIVE_CHECK'			=> $this->_active_check_decode($menu->menus[$id]['active_check']),
					'MENU_STATUS_ACTIVE'		=> ($this->_status($id, true) == 1) ? true : false,
					'MENU_STATUS_NOTCREATED'	=> ($this->_status($id, true) == 2) ? true : false,
					'MENU_STATUS_DISABLED'		=> ($this->_status($id, true) == 3) ? true : false,
					'U_ACTION'					=> $this->u_action . '&amp;action=edit&amp;p=' . $id,
					'S_EDIT'					=> true,
				));
			break;
			
			case 'create':
				$this->page_title = $user->lang['MENU_ADD'];
				
				if($submit)
				{
					if(!check_form_key('form_menu'))
					{
						trigger_error('INVALID_FORM');
					}
					
					$menu_name 			= request_var('menu_name', "");
					$menu_link 			= request_var('menu_link', "");
					$menu_active_check	= request_var('menu_active_check', "");
					$status				= request_var('menu_status', 'active');
					
					$errors = false;
					
					if(!$menu_name)
					{
						$errors = true;
					}
					
					if(!$menu_link)
					{
						$errors = true;
					}
					
					if(!$errors)
					{
						$place = $menu->get_highest_position()+1;

						$sql_array = array(
							'menu_name'			=> $menu_name,
							'menu_link'			=> $menu_link,
							'menu_active_check'	=> $this->_active_check_encode($menu_active_check),
							'menu_disabled'		=> ($status == 'disabled') ? true : false,
							'menu_not_created'	=> ($status == 'not_created') ? true : false,
							'menu_order'		=> $place,
							'menu_outside_link'	=> 0,
						);
						
						$new_menu = $menu->add_item($sql_array);
						
						if(!$new_menu)
						{
							trigger_error(sprintf($user->lang['MENU_ERROR_ADD'], adm_back_link($this->u_action . '?action=create')), E_USER_WARNING);
						}
						
						trigger_error(sprintf($user->lang['MENU_SUCCESS_ADD'], adm_back_link($this->u_action)));
					}
				}
				
				$menu_name = request_var('menu_name', "");
				
				$template->assign_vars(array(
					'MENU_NAME'					=> $menu_name,
					'MENU_LINK'					=> "#",
					'MENU_ACTIVE_CHECK'			=> "",
					'MENU_STATUS_ACTIVE'		=> false,
					'MENU_STATUS_NOTCREATED'	=> false,
					'MENU_STATUS_DISABLED'		=> false,
					'U_ACTION'					=> $this->u_action . '&amp;action=create',
					'S_EDIT'					=> false,
				));
			break;
		}
		
		add_form_key('form_menu');
		
		$this->tpl_name = "acp_menu_creation";
	}
	
	function _active_check_encode($active_string)
	{
		global $menu;
		
		$active_string = str_replace("\n", '', $active_string);
		$active_string = str_replace("\r\n", '', $active_string);
		$active_string = str_replace("\t", '', $active_string);
		
		$active_string = explode(';', $active_string);
		
		$active_array = array();
		
		for($i = 0; $i < count($active_string)-1; $i++)
		{
			$active = explode(':', $active_string[$i]);
			
			$active_array[$active[0]] = $active[1];
		}		
		
		return json_encode($active_array);
	}
	
	function _active_check_decode($json_string)
	{
		$active_array = json_decode($json_string, true);
		
		$return_string = "";
		
		foreach($active_array as $var => $val)
		{
			$return_string .= $var . ":" . $val . ";\n";
		}
		
		return $return_string;
	}
	
	function _move_item($action)
	{
		global $menu;
		
		$menu_id = request_var('m', 0);
		switch($action)
		{
			case 'move_up':
				$menu->push_item_up($menu_id);
				redirect($this->u_action);
			break;
			
			case 'move_down':
				$menu->push_item_down($menu_id);
				redirect($this->u_action);
			break;
		}
	}
	
	function _delete($id, $mode)
	{
		global $menu;
		
		$menu_id = request_var('m', 0);
		
		if(confirm_box(true))
		{
			$menu->remove_item($menu_id);
			redirect($this->u_action);
		}
		else
		{
			$hidden_fields = build_hidden_fields(array(
				'm'			=> $menu_id,
			));
			
			$menu_name = $menu->get_name_from_item($menu_id);
			
			confirm_box(false, 
						"Wilt echt de menu item '<b><i>" . $menu_name . "</i></b>' verwijderen?",
						$hidden_fields);
		}
		
		// $menu->delete_item($menu_id);
		// redirect($this->u_action);
	}
}

?>